import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import autoprefixer from 'autoprefixer';
import path from "path"

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver({ importStyle: "sass" })],
    }),
  ],
  css: {
    // CSS 预处理器
    preprocessorOptions: {
      // 定义全局 SCSS 变量
      scss: {
        // javascriptEnabled: true,
        additionalData: "@use '@/assets/styles/variables.scss' as *;",
      },
    },
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: ['> 0.2%', 'not dead'],
        }),
      ]
    }
  },
  resolve: {
    alias: {
      "@": path.resolve("./src")
    }
  },
  server: {
    force: true,
    port: "7088",  
    host: '0.0.0.0',  
    hot: true, // 开启热更新 
    proxy: { // 代理配置
      "/h5_api": {
        target: "http://119.12.1.1:8085",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/h5_api/, ''),
      }
    }
  },
  build: {
    chunkSizeWarningLimit: 2048,
    reportCompressedSize: false,
    assetsDir: "static",
    rollupOptions: {
      output: {
        manualChunks: {
          vue: ["vue", "vue-router", "pinia"],
          element: ["element-plus", "@element-plus/icons-vue"],
        },
        assetFileNames: (assetInfo) => {
          if (/\.(gif|jpe?g|png|svg)$/.test(assetInfo.name)) {
            // 图片文件放到 'images' 文件夹
            return 'images/[name].[hash][extname]';
          }
          // 其他文件放到 'assets' 文件夹
          return 'assets/[name].[hash][extname]';
        }
      }
    }
  },
  esbuild: {
    pure: ["console.log"],
    drop: ["debugger"],
    legalComments: "none"
  },
})
